const app = getApp();
import { printerList, selectPrinterInfo } from "../../common/api";
Page({
  data: {
    printer_name: "附近的打印机"
  },
  onLoad: function (options) {
  },
  onReady: function () {
    wx.getStorage({
      key: 'printer',
      success: res => {
        this.setData({
          printer_name: `${res.data.printerName}(${res.data.shopName})`,
          printer: res.data,
        })
      }
    })
    this.getPrinterList(0);
  },
  onShow: function () { },
  //获取指定范围内的打印机
  getPrinterList(distance = 1000) {
      wx.getLocation({
        altitude: true,
        isHighAccuracy: true,
        type: "wgs84",
        success: res => {
          console.log(res)
          wx.showLoading({
            title: '加载中...',
            mask: true,
          })
          let data = {
            latitude: res.latitude,
            longitude: res.longitude,
            distance: distance
          }
          this.searchPrinterRequest(data)
        },
        fail: (res) => {
          console.log(res)
        }
      })
  },
  // 查询打印机列表
  searchPrinterRequest(postData) {
    printerList(postData)
      .then(res => {
        wx.hideLoading()
        if (res.code === 200) {
          console.log(res.data)
          this.setData(res.data);
          //查询打印机在线状态
          this.searchPrintersOnlineStatus();
        } else {
          wx.showToast({
            title: res.msg,
          })
        }
      })
      .catch(err => {
        wx.hideLoading()
        console.log('失败', err)
      })
  },
  //查询打印机在线状态
  searchPrintersOnlineStatus() {
    let printers = this.data.printer;
    //status	int	状态：0盒子未上线；1打印机未连接；2打印机已连接
    // 如果查询不到则置为失败
    for (let i = 0; i < printers.length; i++) {
        if(printers[i]["online"] == true){
          printers[i]["onlineText"] = "打印盒子在线";
        }else{
          printers[i]["onlineText"] = "打印盒子未上线";
        }
    }
    this.setData({
      printer: printers
    });
  },
  //选择打印机
  selectPrint: function (e) {
    let printer_id = e.currentTarget.dataset.id;
    this.getPrinter(printer_id);
  },
  getPrinter(printer_id) {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    selectPrinterInfo(printer_id).then(res => {
      wx.showToast({
        title: '选择打印机成功',
        duration: 1000
      })
      wx.navigateBack({
        delta: 1,
      })
    })
    wx.hideLoading()
  },
  onShareAppMessage() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let mini = res.data.mini;
        share = {
          title: mini.title,
          path: "/pages/index/index",
          imageUrl: mini.image,
        }
      }
    })
    return share;
  },
  onShareTimeline() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let momnet = res.data.momnet;
        share = {
          title: momnet.title,
          query: {
            printer_id: this.data.printer.id || 0
          },
          imageUrl: momnet.image,
        }
      }
    })
    return share;
  },
  showCustomerQrcode(e) {
    let index = e.currentTarget.dataset.index;
    let qrcode = this.data.printer[index].tel;
    console.log(qrcode);
    wx.makePhoneCall({
      phoneNumber: qrcode,
    })
  },
  navigatorToShop(e) {
    let index = e.currentTarget.dataset.index;
    let printer = this.data.printer[index];
    console.log(printer);
    let lat = parseFloat(printer.lat);
    let lng = parseFloat(printer.lng);
    wx.openLocation({
      latitude: lat,
      longitude: lng,
    })
  },
  searchPrinter() {
    let menu = ["1公里", "3公里", "5公里", "全部打印机"];
    let menuValue = ["1000", "3000", "5000", "0"];
    wx.showActionSheet({
      alertText: "搜索范围",
      itemList: menu,
      success: res => {
        console.log('tapIndex', parseInt(menuValue[res.tapIndex]));
          this.getPrinterList(parseInt(menuValue[res.tapIndex]));
      }
  })
  }
})